<?php
/** 
 * Classified_model
 * This class handles model work necessary for logging in user
 * 
 * @author Ray Nowell
 *	
 */ 
class Classified_model extends CI_Model {

	function Classified_model()
	{
		parent::__construct();
	}
	
    function Get_classifieds() {
        
		$select_fields =	"	 member_car.car_id
                                ,CONCAT('<img src=\"./member_car_gallery/thumbnail/',pic_name,'_thumb.jpg\"/>') as pic_name 
								,nickname
								,year
								,model.short_name as model
								,engine.short_name as engine
                                ,CONCAT('$', FORMAT(price, 2)) AS price
                                ,first_name AS seller";
		$this->db->select($select_fields, false);
		$this->db->from('member');
        $this->db->join('member_car', 'member.member_id = member_car.member_id');
		$this->db->join('member_car_gallery', 'member_car.car_id = member_car_gallery.car_id and member_car_gallery.is_display_photo = 1','left');
		$this->db->join('lookup AS model', 'member_car.model_id = model.lookup_id', 'left'); 
		$this->db->join('lookup AS engine', 'member_car.engine_id = engine.lookup_id', 'left'); 
		$this->db->where('for_sale',true);
		$query = $this->db->get();
		 
		return $query->result_array();
    }
    
	function Load_classified($car_id)
	{
		$member_id = $this->session->userdata('member_id');
		$select_fields =	"	 nickname
								,year
								,model.short_name as model
								,engine.short_name as engine
								,CONCAT('$', FORMAT(price, 2)) AS price
								,notes
                                ,first_name AS seller
                                ,CASE contact.short_name
                                    WHEN 'Phone' THEN member.phone
                                    WHEN 'Email' THEN member.email
                                    WHEN 'Facebook' THEN member.facebook_username
                                    WHEN 'Twitter' THEN member.twitter_username
                                    ELSE member.email
                                END AS contact";
		$this->db->select($select_fields, false);
		$this->db->from('member');
        $this->db->join('member_car', 'member.member_id = member_car.member_id');
		$this->db->join('lookup AS model', 'member_car.model_id = model.lookup_id', 'left'); 
		$this->db->join('lookup AS engine', 'member_car.engine_id = engine.lookup_id', 'left'); 
        $this->db->join('lookup AS contact', 'member.contact_method_id = contact.lookup_id', 'left'); 
		$this->db->where('car_id', $car_id);
		$query = $this->db->get();
		
		return $query->row();
	}
    
	
	
}

/* End of file classified_model.php */
/* Location: ./system/application/models/classified_model.php */